function InputControls(){
	
	this.key = { left:37, up:38, right:39, down:40 };
	this.event = { raw: {current:null, prev:null}, sys: null };
	
	this.controller = null;
	this.eventCounter = 0;
	
	this.onKeyPress = function(e){
		switch (e.keyCode){
			case this.key.left:	
				this.event.sys = "onKeyPress";
				this.controller.wakeUp();
				axis_y += rotationSpeed;
				break;
			case this.key.up:
				this.event.sys = "onKeyPress";
				this.controller.wakeUp();
				axis_x -= rotationSpeed;
				break;
			case this.key.right:
				this.event.sys = "onKeyPress";
				this.controller.wakeUp();
				axis_y -= rotationSpeed;
				break;
			case this.key.down:
				this.event.sys = "onKeyPress";
				this.controller.wakeUp();
				axis_x += rotationSpeed;
				break;
			default:
				break;
		}
	};
	
	this.onKeyUp = function(){
		this.controller.sleep();
	};
	
	this.setMouseEvent = function(e){
		this.event.raw.current =  e;
		this.mouseX = e.pageX - e.currentTarget.offsetLeft;
		this.mouseY = e.pageY - e.currentTarget.offsetTop;
		
		if(this.event.raw.prev == null){
			this.event.raw.prev =  this.event.raw.current; 
			return true; 
		}
		
		if(this.event.raw.prev.type == "mousedown" && this.event.raw.current.type == "mouseup"){
			this.event.sys = "onClick";
			this.notifyController();
		}
		
		if(this.event.raw.prev.type == "mousedown" && this.event.raw.current.type == "mousemove"){
			this.event.sys = "onDragStart";
			this.notifyController();
		}
		
		if(this.event.sys == "onDragStart" && this.event.raw.current.type == "mousemove"){
			this.event.sys = "onDrag";
			this.notifyController();
		}
		
		if(this.event.sys == "onDrag" && this.event.raw.current.type == "mousemove"){
			this.event.sys = "onDrag";
			this.notifyController();
		}
		
		if(this.event.raw.prev.type == "mousemove" && this.event.raw.current.type == "mouseup" && this.event.sys == "onDrag"){
			this.event.sys = "onDragEnd";
			this.notifyController();
		}
		
		this.event.raw.prev =  this.event.raw.current;
		return true;
	};
	
	this.resetDragEvent = function(){
		this.event.sys = null;
	};
	
	this.notifyController = function(){
		var sysEvent = new SysMouseEvent;
		sysEvent.id = this.eventCounter;
		sysEvent.eventType = this.event.sys;
		sysEvent.mouseX = this.mouseX;
		sysEvent.mouseY = this.mouseY;
		this.controller.UpdateFromInput(sysEvent);
		this.eventCounter++;
	};
	
	this.playerEvent = function(e){
		this.event.sys = 'player'+e;
		this.notifyController();
	};
	
	this.setController = function(c){
		this.controller = c;
	};
	
	
}

var Inputs = new InputControls();

$(document).ready(function() {
	$(document).keyup(function(){
		Inputs.onKeyUp();	
	});
	$(document).keypress(function(e){
		Inputs.onKeyPress(e);
	});
	
	$('#thecanvas').bind('mousemove mousedown mouseup', function(event) {
		Inputs.setMouseEvent(event);
	});
	
	$('#thecanvas').bind('mouseleave', function(event) {
		Inputs.resetDragEvent();
	});
	
	/* =========== player =========== */
	
	$('#playerBackward').bind('click', function(event) {
		Inputs.playerEvent('Backward');
	});
	
	$('#playerForward').bind('click', function(event) {
		Inputs.playerEvent('Forward');
	});
	
	$('#playerNext').bind('click', function(event) {
		Inputs.playerEvent('Next');
	});
	
	$('#playerPrev').bind('click', function(event) {
		Inputs.playerEvent('Prev');
	});
});